package com.jy.novel.dao;

import com.jy.novel.common.JdbcHelper;
import com.jy.novel.common.Page;
import com.jy.novel.pojo.Novel;
import com.jy.novel.pojo.form.NovelAddForm;
import com.jy.novel.pojo.form.NovelEditForm;
import com.jy.novel.pojo.form.NovelQueryForm;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jihb on 4/14/16.
 */
@Repository
public class NovelDao {

    @Autowired
    JdbcHelper jdbcHelper;

    /**
     * 分页查询小说 返回page
     * @param queryForm
     * @param curPage
     * @param pageSize
     * @return
     */
    public Page<Novel> findNovelPage(NovelQueryForm queryForm,int curPage,int pageSize){
        StringBuilder sb = new StringBuilder();
        sb.append("select id,name,rootCategoryId,categoryId,userId,remark,viewCount,praiseCount,createTime,updateTime from t_novel where 1=1");
        List params = new ArrayList();
        if (queryForm != null){
            if(queryForm.getCategoryId() != null){
                sb.append(" and categoryId=? ");
                params.add(queryForm.getCategoryId());
            }
            if (queryForm.getRootCategoryId() != null){
                sb.append(" and rootCategoryId=? ");
                params.add(queryForm.getRootCategoryId());
            }
            if (queryForm.getUserId() != null){
                sb.append(" and userId=? ");
                params.add(queryForm.getUserId());
            }
            if (StringUtils.isNotBlank(queryForm.getQueryKey())){
                sb.append(" and (name like ? or remar like ? )");
                params.add("%"+queryForm.getQueryKey()+"%");
                params.add("%"+queryForm.getQueryKey()+"");
            }
        }
        sb.append(" order by updateTime desc,createTime desc");
        return jdbcHelper.queryForPageBean(sb.toString(),params.toArray(),Novel.class,curPage,pageSize);
    }

    /**
     * 分页查询小说 返回list
     * @param queryForm
     * @param curPage
     * @param pageSize
     * @return
     */
    public List<Novel> findNovelList(NovelQueryForm queryForm, int curPage, int pageSize){
        StringBuilder sb = new StringBuilder();
        sb.append("select id,name,rootCategoryId,categoryId,userId,remark,viewCount,praiseCount,createTime,updateTime from t_novel where 1=1");
        List params = new ArrayList();
        if (queryForm != null){
            if(queryForm.getCategoryId() != null){
                sb.append(" and categoryId=? ");
                params.add(queryForm.getCategoryId());
            }
            if (queryForm.getRootCategoryId() != null){
                sb.append(" and rootCategoryId=? ");
                params.add(queryForm.getRootCategoryId());
            }
            if (queryForm.getUserId() != null){
                sb.append(" and userId=? ");
                params.add(queryForm.getUserId());
            }
            if (StringUtils.isNotBlank(queryForm.getQueryKey())){
                sb.append(" and (name like ? or remar like ? )");
                params.add("%"+queryForm.getQueryKey()+"%");
                params.add("%"+queryForm.getQueryKey()+"");
            }
        }
        sb.append(" order by updateTime desc,createTime desc");
        return jdbcHelper.queryForListBean(sb.toString(),params.toArray(),Novel.class,curPage,pageSize);
    }

    /**
     * 添加小说
     * @param addForm
     * @return
     */
    public boolean addNovel(NovelAddForm addForm){
        StringBuilder sb = new StringBuilder();
        sb.append("insert into t_novel(name,rootCategoryId,categoryId,userId,remark,viewCount,praiseCount,createTime,updateTime)");
        sb.append(" values(?,?,?,?,?,?,?,?,?)");
        List params = new ArrayList();
        params.add(addForm.getName());
        params.add(addForm.getRootCategoryId());
        params.add(addForm.getCategoryId());
        params.add(addForm.getUserId());
        params.add(addForm.getRemark());
        params.add(0);
        params.add(0);
        Date now = new Date();
        params.add(now);
        params.add(now);
        return jdbcHelper.update(sb.toString(),params.toArray())>0;
    }

    /**
     * 修改小说
     * @param editForm
     * @param id
     * @return
     */
    public boolean updateNovel(NovelEditForm editForm,long id){
        StringBuilder sb = new StringBuilder();
        sb.append("update t_novel set updateTime=?");
        List params = new ArrayList();
        params.add(new Date());
        if (editForm.getName() != null){
            sb.append(",name=?");
            params.add(editForm.getName());
        }
        if (editForm.getRemark() != null){
            sb.append(",remark=?");
            params.add(editForm.getRemark());
        }
        if (editForm.getRootCategoryId() != null){
            sb.append(",rootCategoryId=?");
            params.add(editForm.getRootCategoryId());
        }
        if (editForm.getCategoryId() != null){
            sb.append(",categoryId=?");
            params.add(editForm.getCategoryId());
        }
        sb.append(" where id=?");
        params.add(id);
        return jdbcHelper.update(sb.toString(),params.toArray())>0;
    }

    /**
     * 删除小说
     * @param id
     * @return
     */
    public boolean deleteNovel(long id){
        return jdbcHelper.update("delete from t_novel where id=?",new Object[]{id})>0;
    }
}
